<?php defined('_XR_EXEC') or die();
/**
 * URL  class.
 *
 * 
 * by zhoupeng 2012-3-23
 *
 */
class url {
	# 编码URL
	public static function encodeUrlArgs($args) {
		if(empty($args))return '';
		$str = '';
		if(!is_array($args))$args=(array)$args;
		foreach ($args as $key => $value) {
			$str .= '&' . rawurlencode($key) . '=' . rawurlencode($value);
		}
		return substr($str,1);
	}
	# 为了兼容以前的写法
	public static function build($m,$a,$args=array()){
		$args = self::encodeUrlArgs($args);
		if($args != ''){
			 $args = '?'.$args;
		}
		return self::site($m.'/'.$a).$args;
	}



	# 得到url路径 or 全路径
	public static function site($uri='',$abs=true){
		$hosts = self::base($abs);
		$url=$hosts.ltrim($uri,'/');
		return $url;
	}
 
	public static function base($enforceAll=true,$protocol=true){
		$http = factory::getcfg('site_protocol','http');
		$c_path = str_replace("\\","/",XR_APP_PUBLIC) . "/";
		$pathpart = str_replace("//","/",str_replace($_SERVER['DOCUMENT_ROOT'],"",$c_path)); 
		if (substr($pathpart,0,1) != "/") {
			$pathpart = "/" . $pathpart;
		}
		if(!$enforceAll){
			return $pathpart;
		}else{
			if($protocol === true){
				return $http."://".$_SERVER['HTTP_HOST'] . $pathpart;
			}else{
				return $_SERVER['HTTP_HOST'] . $pathpart;
			}	
		}
	}
	
	
	public static function selfUri($enforceAll=true){
		$http = factory::getcfg('site_protocol','http');
		$pathpart = $_SERVER['REQUEST_URI'];
		if (substr($pathpart,0,1) != "/") {
			$pathpart = "/" . $pathpart;
		}
		if(!$enforceAll){
			return $pathpart;
		}else{
			return $http."://".$_SERVER['HTTP_HOST'] . $pathpart;
		}
	}
	public static function selfUrl($enforceAll=true){
		$http = factory::getcfg('site_protocol','http');
		$pathpart = $_SERVER['REDIRECT_URL'];
		if (substr($pathpart,0,1) != "/") {
			$pathpart = "/" . $pathpart;
		}
		if(!$enforceAll){
			return $pathpart;
		}else{
			return $http."://".$_SERVER['HTTP_HOST'] . $pathpart;
		}
	}
 
  
	//页面跳转
	public static function jump($w){
		header("Location: ".$w);
		exit();
	}

	# alert形式提示，然后跳转指定页面
	public static function msgalert($message = '', $gotourl = '(-1)', $time = 5 ){

		$file = XR_ENGINE_LIB.'utils' . DS . 'msgalert.php';
		if ($gotourl==='(-1)' || $gotourl==='-1') {
			$gotourl = 'history.go(-1);';
		} elseif($gotourl==='window.close' || $gotourl==='window.close()' || $gotourl==='close'  || $gotourl==='close()' ) {
			$gotourl='window.close();';
		}else{
			$gotourl = ((('' . 'self.location.href=\'') . $gotourl) . '\';');
		}
		if (file_exists($file)) {
			include ($file);
			exit();
		} else {
			die($message);
		}
	}

	# 错误提示
	public static function msgBox($error = '', $gotourl = '(-1)', $time = 5 ) {
	
		if(R::singleton()->gets['reqtype'] === "json"){
			exit($error);
		}
		if ($gotourl==='(-1)' || $gotourl==='-1') {
			$gotourl = 'javascript:history.go(-1)';
			$gotourl_js = 'history.go(-1)';
		} elseif($gotourl==='window.close' || $gotourl==='window.close()' || $gotourl==='close'  || $gotourl==='close()' ) {
			$gotourl = 'javascript:window.close()';
			$gotourl_js='window.close()';
		}else{
			$gotourl_js = ((('' . 'self.location.href=\'') . $gotourl) . '\';');
		}
		$file = XR_ENGINE_LIB.'utils' . DS . 'msgbox.php';
		if (file_exists($file)) {
			include ($file);
			exit();
		} else {
			die($error);
		}
	}


}

?>